<?php

namespace app\api\behavior;

use app\admin\model\Region;
use app\common\model\Order;
use app\common\model\User;
use think\Db;

class Sw
{
    /**
     * 用户初始化
     */
    public function userInitSuccessed(&$user)
    {
        $user->wechat = Db::name('UserThird')->where(['user_id' => $user->id, 'platform' => 'wechat'])->column(1, 'apptype');
        $user->income = number_format(Order::where(['user_id' => $user->id, 'status' => 2])->sum('total_money'), 2);
//        $user->wechat_tmpl_ids = ['ZZSV3jhD-2K638qbFvogKj_Qx0H1Uufr_-WJT8Mc3ws'];
    }

    /**
     * 用户区域处理
     */
    public function userRegionHandle(&$params)
    {
        $user       = isset($params['user']) ? $params['user'] : [];
        $address    = isset($params['address']) ? $params['address'] : [];
        if (!empty($address['lat']) && !empty($address['lng']) && is_object($user) && empty($user->region_id) && !empty($user->id)) {
            if ($region_id = Region::siteToRegion(['lat' => $address['lat'], 'lng' => $address['lng']])) {
                User::where(['id' => $user->id])->update(['region_id' => $region_id]);
                $user->region_id = $region_id;
            }
        }
    }
}